#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

void ChildWrite(int fd)
{
    std::cout << "Child process writing to pipe" << std::endl;
    std::string message = "Hello from child";
    write(fd, message.c_str(), message.size() + 1);
}

void FatherRead(int fd)
{
    char buffer[100];
    std::cout << "Father process reading from pipe" << std::endl;
    read(fd, buffer, 100);
    std::cout << "Message from child: " << buffer << std::endl;
}

int main()
{
    int pipefd[2];
    int n = pipe(pipefd);
    if (n < 0)
    {
        std::cout << "Error in creating pipe" << std::endl;
        return 1;
    }
    pid_t id = fork();
    if (id == 0)
    {
        close(pipefd[0]);
        ChildWrite(pipefd[1]);
        close(pipefd[1]);
        exit(0);
    }

    close(pipefd[1]);
    FatherRead(pipefd[0]);
    waitpid(id, NULL, 0);
    close(pipefd[0]);
    return 0;
}